#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using Gizmox.WebGUI.Common.Resources;
using PLACEHR.Web.Categories.SystemSetting.Forms;
using PLACEHR.Web.Utilities;

#endregion

namespace PLACEHR.Web.Categories.SystemSetting.Controls
{
    public partial class DepartmentControl : UserControl, IHostedApplication
    {
        public DepartmentControl()
        {
            InitializeComponent();
            Retrieve();
        }

        #region IHostedApplication

        public void InitializeApplication()
        {
        }

        public HostedToolBarElement[] GetToolBarElements()
        {
            ArrayList objElements = new ArrayList();
            objElements.Add(new HostedToolBarButton(LanLayer.GetTrans(20), LanLayer.GetTrans(20), new IconResourceHandle("Outlook.NewItem.gif"), "Retrieve"));
            objElements.Add(new HostedToolBarSeperator());
            objElements.Add(new HostedToolBarButton(LanLayer.GetTrans(21), LanLayer.GetTrans(21), new IconResourceHandle("CRM.ico_16_5006.gif"), "OpenSelected"));
            objElements.Add(new HostedToolBarSeperator());
            objElements.Add(new HostedToolBarButton(LanLayer.GetTrans(22), LanLayer.GetTrans(22), new IconResourceHandle("CRM.ico_16_1070.gif"), "Add"));
            objElements.Add(new HostedToolBarSeperator());
            objElements.Add(new HostedToolBarButton(LanLayer.GetTrans(23), LanLayer.GetTrans(23), new IconResourceHandle("CRM.ico_16_5010.gif"), "DeleteSelected"));

            return (HostedToolBarElement[])objElements.ToArray(typeof(HostedToolBarElement));
        }

        public void OnToolBarButtonClick(HostedToolBarButton objButton, EventArgs objEvent)
        {
            string strAction = (string)objButton.Tag;
            switch (strAction)
            {
                case "Retrieve":
                    Retrieve();
                    break;
                case "OpenSelected":
                    OpenDetailForm();
                    break;
                case "Add":
                    AddDetailForm();
                    break;
                case "DeleteSelected":
                    if (lvwData.CheckedItems.Count > 0)
                    {
                        MessageBox.Show("Please make sure you want to delete selected Departments!!", "Prompt", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                                        (obj, arg) =>
                                        {
                                            if (((MessageBoxWindow)obj).DialogResult == DialogResult.Yes)
                                            {
                                                DeleteSelected();
                                                Retrieve();
                                            }
                                        });
                    }
                    else
                    {
                        MessageBox.Show("Please select which catagory you want to delete!!");
                    }
                    break;
            }
        }

        #endregion

        #region Method

        private void DeleteSelected()
        {
            foreach (ListViewItem lvwItem in lvwData.CheckedItems)
            {
                Department c = lvwItem.Tag as Department;
                c.IsDeleted = true;
                //DataRow item = lvwItem.Tag as DataRow;
                //SqlHelper.ExecuteNonQuery(Global.ConnectionString, CommandType.Text, string.Format("delete from [Catagory] where id = {0}", item["ID"].ToString()));
            }

            Global.Context.SaveChanges();
        }

        private void OpenDetailForm()
        {
            if (lvwData.SelectedItem != null)
            {
                Department item = lvwData.SelectedItem.Tag as Department;
                DepartmentForm form = new DepartmentForm();
                form.currItem = item;
                form.FormClosed += new Gizmox.WebGUI.Forms.Form.FormClosedEventHandler(form_FormClosed);
                form.ShowDialog();
            }
        }

        void form_FormClosed(object sender, FormClosedEventArgs e)
        {
            DepartmentForm form = sender as DepartmentForm;
            if (form.DialogResult == DialogResult.OK)
            {
                Retrieve();
            }
        }

        private void AddDetailForm()
        {
            DepartmentForm form = new DepartmentForm();
            form.FormClosed += new Gizmox.WebGUI.Forms.Form.FormClosedEventHandler(form_FormClosed);
            form.ShowDialog();
        }

        private void Retrieve()
        {
            List<Department> list = Global.Context.Departments.Where(o => o.IsDeleted == false).ToList();

            lvwData.Items.Clear();
            lvwData.BeginUpdate();
            int No = 1;
            foreach (var item in list)
            {
                ListViewItem lvwItem = new ListViewItem("");
                lvwItem.SubItems.AddRange(new string[6]);

                lvwItem.SubItems[0].Text = (No++).ToString();
                lvwItem.SubItems[1].Text = item.DepartmentCode;
                lvwItem.SubItems[2].Text = item.DepartmentNumber;
                lvwItem.SubItems[3].Text = item.DepartmentName;
                
                lvwItem.Tag = item;

                lvwData.Items.Add(lvwItem);
            }
            lvwData.EndUpdate();
        }

        #endregion

        #region Event

        private void lvwData_DoubleClick(object sender, EventArgs e)
        {
            OpenDetailForm();
        }

        #endregion
    }
}